import { defineStore } from 'pinia';
import { ErrorMessageMode } from '/#/axios';
import {
  AddRoleAuthParams,
  AddShopRolesParams,
  deleteRoleAuthParams,
  GetAuthListParams,
  PutShopRolesParams,
} from '/@/api/shopper/model/shopRolesModel';
import {
  addRoleAuthApi,
  addShopRolesApi,
  deleteRoleAuthApi,
  deleteShopRolesApi,
  getAuthListApi,
  putSingleShopRoleApi,
  shopRolesApi,
  singleShopRoleApi,
} from '/@/api/shopper/shopRoles';
import { TreeDataItem } from 'ant-design-vue/es/tree/Tree';

interface stateInterface {
  authData: TreeDataItem[];
}

export const useShopRoleStore = defineStore({
  id: 'shop-role',
  state: (): stateInterface => ({
    authData: [],
  }),
  getters: {},
  actions: {
    /**
     * @description: get role list
     */
    async roleList(): Promise<Object[] | any> {
      try {
        const data = await shopRolesApi();
        const result = data.map((item: any) => {
          item['personAuth'] = item.children;
          delete item.children;
          return item;
        });
        return result;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: add role
     */
    async addRole(
      params: AddShopRolesParams & { mode?: ErrorMessageMode },
    ): Promise<Object[] | any> {
      try {
        const { mode, ...UpdateUserRole } = params;
        const data = await addShopRolesApi(UpdateUserRole, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: delete role
     */
    async deleteRole(params: { id: number; mode?: ErrorMessageMode }): Promise<null> {
      try {
        const { id, mode } = params;
        const data = await deleteShopRolesApi(id, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: get single role
     */
    async singleRole(params: { id: number; mode?: ErrorMessageMode }): Promise<Object | any> {
      try {
        const { id, mode } = params;
        const data = await singleShopRoleApi(id, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: put single role
     */
    async submitSingleRole(
      params: PutShopRolesParams & { mode?: ErrorMessageMode },
    ): Promise<Object | any> {
      try {
        const { mode, ...PutShopRolesParams } = params;
        const data = await putSingleShopRoleApi(PutShopRolesParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: add role auth
     */
    async addRoleAuth(params: AddRoleAuthParams & { mode?: ErrorMessageMode }): Promise<null> {
      try {
        const { mode, ...AddRoleAuthParams } = params;
        const data = await addRoleAuthApi(AddRoleAuthParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: get auth list
     */
    async authList(params: GetAuthListParams & { mode?: ErrorMessageMode }): Promise<Object | any> {
      try {
        const { mode, ...GetAuthListParams } = params;
        const data = await getAuthListApi(GetAuthListParams, mode);
        if (GetAuthListParams.type == 'tree') {
          function getTreeData(data: Array<Object>) {
            if (!data.length) return [];
            return data.map((item: any) => {
              return {
                title: item.authName,
                key: item.id,
                children: item.children ? getTreeData(item.children) : [],
              };
            });
          }
          const antTreeData = getTreeData(data);
          this.authData = antTreeData;
          return antTreeData;
        }
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
    /**
     * @description: delete role Auth
     */
    async deleteRoleAuth(
      params: deleteRoleAuthParams & { mode?: ErrorMessageMode },
    ): Promise<Object | any> {
      try {
        const { mode, ...deleteRoleAuthParams } = params;
        const data = await deleteRoleAuthApi(deleteRoleAuthParams, mode);
        return data;
      } catch (error) {
        return Promise.reject(error);
      }
    },
  },
});
